﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Aeon.Configuration;
using Microsoft.Win32;

namespace Aeon.DiskImages.Views
{
    /// <summary>
    /// Displays the UI for configuration a host CD-ROM drive.
    /// </summary>
    [DriveConfigurationView(typeof(HostCDDriveConfiguration))]
    public partial class HostDriveView : UserControl, IDriveConfigurationView
    {
        /// <summary>
        /// DriveConfiguration dependency property.
        /// </summary>
        public static readonly DependencyProperty DriveConfigurationProperty = DependencyProperty.Register("DriveConfiguration", typeof(HostCDDriveConfiguration), typeof(HostDriveView), new PropertyMetadata(null));

        /// <summary>
        /// Gets the available CD-ROM drives on the system.
        /// </summary>
        public static IEnumerable<string> AvailableDrives
        {
            get { return DriveInfo.GetDrives().Where(d => d.DriveType == DriveType.CDRom).Select(d => d.Name.TrimEnd('\\')); }
        }

        public HostDriveView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets or sets the drive configuration being viewed. This is a dependency property.
        /// </summary>
        public HostCDDriveConfiguration DriveConfiguration
        {
            get { return (HostCDDriveConfiguration)GetValue(DriveConfigurationProperty); }
            set { SetValue(DriveConfigurationProperty, value); }
        }

        /// <summary>
        /// Gets or sets the drive configuration being viewed.
        /// </summary>
        IDriveConfiguration IDriveConfigurationView.DriveConfiguration
        {
            get { return this.DriveConfiguration; }
            set { this.DriveConfiguration = (HostCDDriveConfiguration)value; }
        }

        /// <summary>
        /// Handles the Click event of the SaveIsoButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void SaveIsoButton_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new SaveFileDialog()
            {
                Title = "Save ISO Image...",
                Filter = "ISO Image Files|*.iso|All Files|*.*",
                DefaultExt = "iso"
            };

            if(dialog.ShowDialog() == true)
            {
                var volume = this.driveComboBox.SelectedItem.ToString();
                HostCDDrive.CreateIsoImage(volume, dialog.FileName);
            }
        }
    }
}
